function infoList = MatStock( id,ymd )
  %% MatStock:从新浪获取股票历史数据
  % id:股票id (sh000001) (上海 .sh 深圳 .sz)
  % ymd 日期 {'20150101','20160101'}
  %
  % infoList {
  %   date:日期
  %   open:开盘价
  %   high:最高价
  %   close:收盘价
  %   low:最低价
  %   volume:成交量
  % }
  %
  % * url

  tmpXml = 'stockTmp.xml';
  readUrlToFile(id,ymd,tmpXml);
  infoList = readXmlFromFile(tmpXml);

end

function readUrlToFile( id,ymd,fileName )

  % url
  url ='http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=%s&begin_date=%s&end_date=%s';
  str = urlread(sprintf(url,id,ymd{1},ymd{2}));

  fid = fopen(fileName,'w');
  fprintf(fid,str);
  fclose(fid);

end

function infoList = readXmlFromFile(fileName)
  try
    xDoc = xmlread(fileName);
  catch
    error('Failed to read XML file %s.',fileName);
  end

  contentList = xDoc.getElementsByTagName('content');
  size = contentList.getLength;

  infoList = struct('date',cell(size,1),...
  'open', zeros(1,1),...
  'high', zeros(1,1),...
  'close', zeros(1,1),...
  'low', zeros(1,1),...
  'volume', zeros(1,1));

  for i = 0 : size-1
    attrs = contentList.item(i).getAttributes();
    for j = 0 : attrs.getLength-1
      item =  attrs.item(j);
      switch item.getNodeName().toCharArray()
      case 'd'
        % 日期
        infoList(i+1).date = item.getValue().toCharArray();
      case 'o'
        % 开盘价
        infoList(i+1).open = str2double(item.getValue());
      case 'h'
        % 最高价
        infoList(i+1).high = str2double(item.getValue());
      case 'c'
        % 收盘价
        infoList(i+1).close = str2double(item.getValue());
      case 'l'
        % 最低价
        infoList(i+1).low = str2double(item.getValue());
      case 'v'
        % 成交量
        infoList(i+1).volume = str2double(item.getValue());
      end
    end
  end
end
